Ext.define('TS.view.base.ProductSearchDialog', {
    extend: 'Ext.window.Window',
    alias : 'widget.productdialog',
	mixins: {
        observable: 'Ext.util.Observable'
    },
    constructor: function () {
    	this.callParent();
        this.addEvents(
        {'selected':true}
        );
    },
    title : '商品资料查询',
    width:900,
    layout:'fit',
    height:400,
    config: {
    	val:"",//设置初始查询条件
    	isOneShow:true//只有一条记录是否显示窗口
    },
    modal:true,
    initComponent: function() {
    	Ext.apply(this, {
    		tbar :[
				{
					xtype: 'triggerfield',
					labelWidth :60,
					fieldLabel:'查询条件:',
					width:400,
					margin:'0,0,10,10',
	    	    	emptyText:'请输入商品编码或名称',
	    	    	trigger1Cls:Ext.baseCSSPrefix + "form-search-trigger",
	    	    	onTriggerClick:function(e){
	    	    		e.keyCode=e.ENTER;//设置为Enter事件
	    	    		this.fireEvent('specialkey',this,e);//点客户查询图标执行搜索
	    	    	},
	    	    	listeners: {
						specialkey:{
							fn:function(field,e){
								//回车客户搜索事件
								if(e.keyCode==e.ENTER){
									
									var txt=this.up('toolbar').down('triggerfield').value;
									//Ext.Msg.alert('提示',"field是否为空?"+Ext.isEmpty(field)+"<br>Ext.isEmpty('',true)的值:"+Ext.isEmpty(txt,true)+"<br>Ext.isEmpty('',false)的值"+Ext.isEmpty(txt,false));
									if(!Ext.isEmpty(txt)){
										var grid=this.up('window').items.items[0];
										grid.store.removeAll();
										this.up('window').down('pagingtoolbar').getStore().removeAll();
										grid.store.on("beforeload",function(){
									        Ext.apply(grid.store.proxy.extraParams, {val:txt});
									    });
									    grid.store.loadPage(1,{
									    	callback:function(records, operation, success){
								    			if(records){
									    			if(records.length>0){
									    				//有数据默认选择第一条并获得焦点?
									    				grid.getSelectionModel().select(records[0],true);
									    				field.setValue("");
									    				grid.focus(100);
									    			}else{
									    				//没有数据清空输入框并获得焦点
									    				field.setValue("");
									    				field.focus('',100);
									    			}
								    			}
								    		},
									   		scope: this
									    });
										/*grid.store.load({
											params:{val:txt,page:1,start:0},
								    		callback:function(records, operation, success){
								    			if(records){
									    			if(records.length>0){
									    				//有数据默认选择第一条并获得焦点?
									    				grid.getSelectionModel().select(records[0],true);
									    				field.setValue("");
									    				grid.focus(100);
									    			}else{
									    				//没有数据清空输入框并获得焦点
									    				field.setValue("");
									    				field.focus('',100);
									    			}
								    			}
								    		},
									   		scope: this
										});*/
									}
								}
							}
						}
					}
				}
    		],
    		items:[
        		Ext.create('TS.view.base.ProductSimpleList',{
        			autoScroll: true,
        			title:'',
        			closable: false,
        			listeners: {
        				//双击
						itemdblclick:{
							fn:function(grid, record, item, index, e, eOpts){
								var records=[];
								records.push(record);
								var win=this.up('window');
								win.fireEvent('selected',records);
	                			win.hide();
							}
						},
						//回车
						cellkeydown:{
							fn:function(grid, td, cellIndex, record, tr, rowIndex, e, eOpts ){
								if(e.keyCode==e.ENTER){
									var records=[];
									records.push(record);
									var win=this.up('window');
									win.fireEvent('selected',records);
		                			win.hide();
								}
							}
						}
					}        			
        		})
        	],
        	buttons:[
	        	{
	                text: '确认',
	                scope: this,
	                handler:function(){
	                	var grid=this.items.items[0];
	                	var records=grid.getSelectionModel().getSelection();
	                	//确认没有选择行则不执行.
	                	if(records.length>0){
	                		this.fireEvent('selected',records);
	                		this.hide();
	                	}
	                	
	                }
	            },
	            {
	                text: '取消',
	                scope: this,
	                handler: this.close
	            }
	        ]
    	});

        this.callParent(arguments);
    },
	listeners: {
		render:{
			fn:function(){
				var grid=this.items.items[0];
				var str=this.getVal();
				
				if(!Ext.isEmpty(str)){
					grid.store.on("beforeload",function(){
				        Ext.apply(grid.store.proxy.extraParams, {val:str});
				    });
					grid.store.loadPage(1,{
						params:{val:str},
			    		callback:function(records, operation, success){
			    			if(records){
			    				//如果只有一条记录并且只有IsOneShow为false.
				    			if(records.length==1 && !this.getIsOneShow()){
				    				this.fireEvent('selected',records);
			                		this.hide();
				    			}
				    			if(records.length>0){
				    				//多条记录默认选择第一条
				    				grid.getSelectionModel().select(records[0],true);
									
				    			}
			    			}
			    		},
				   		scope: this
					});					
				}
			}
		},
		show:{
			fn:function(){
				this.down('triggerfield').setValue(this.getVal());
				//var grid=this.items.items[0];
				//如有数据默认选择第一行并设置焦点为Grid,支持上下键操作和回车,没有数据焦点停在条件输入框
				/*if(grid.store.getTotalCount()>0){
					grid.focus(100);
				}else{
					var txtfield=this.down('triggerfield');
					txtfield.focus('',100);
				}*/
				
			}
		}
	}
});